<template>
  <div id="map-container"></div>
</template>
<script>
import { Map, View } from 'ol';
import { Tile as TileLayer } from 'ol/layer';
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import { Cluster } from 'ol/source';
import Text from 'ol/style/Text'
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Point from 'ol/geom/Point';
import Feature from 'ol/Feature';
import {Icon, Style} from "ol/style";
import { get } from 'ol/proj';
import { getWidth, getTopLeft } from 'ol/extent';
import { WMTS } from 'ol/source';
import WMTSTileGrid from 'ol/tilegrid/WMTS';
import { defaults as defaultControls } from 'ol/control';

export const projection = get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
for (let z = 0; z < 19; ++z) {
  resolutions[z] = size / Math.pow(2, z);
}

let map;
export default {
  data() {
    return {

    }
  },
  mounted(){
    this.initMap() // 加载矢量底图
  },
  methods:{
    initMap() {
      const KEY = '你申请的KEY'

      map = new Map({
        target: 'map-container',
        layers: [
          // 底图
          new TileLayer({
            source: new WMTS({
              url: `http://t{0-6}.tianditu.com/vec_c/wmts?tk=${KEY}`,
              layer: 'vec', // 矢量底图
              matrixSet: 'c', // c: 经纬度投影 w: 球面墨卡托投影
              style: "default",
              crossOrigin: 'anonymous', // 解决跨域问题 如无该需求可不添加
              format: "tiles", //请求的图层格式，这里指定为瓦片格式
              wrapX: true, // 允许地图在 X 方向重复（环绕）
              tileGrid: new WMTSTileGrid({
                origin: getTopLeft(projectionExtent),
                resolutions: resolutions,
                matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']
              })
            })
          }),
          // 标注
          new TileLayer({
            source: new WMTS({
              url: `http://t{0-6}.tianditu.com/cva_c/wmts?tk=${KEY}`,
              layer: 'cva', //矢量注记
              matrixSet: 'c',
              style: "default",
              crossOrigin: 'anonymous',
              format: "tiles",
              wrapX: true,
              tileGrid: new WMTSTileGrid({
                origin: getTopLeft(projectionExtent),
                resolutions: resolutions,
                matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']
              })
            })
          })
        ],
        view: new View({
          center: [118.958366,32.119577],
          projection: projection,
          zoom: 13,
          maxZoom: 17,
          minZoom: 1
        }),
        //加载控件到地图容器中
        controls: defaultControls({
          zoom: false,
          rotate: false,
          attribution: false
        })
      });

      let features = this.createFeatures();
      const source = new VectorSource({
        features: features,
      });

      const clusterSource = new Cluster({
        source: source,
        distance: 80,
      });

      const vectorLayer = new VectorLayer({
        source: clusterSource,
        style: (feature) => {
          const size = feature.get('features').length;
          return new Style({
            image: new CircleStyle({
              radius: 25,
              stroke: new Stroke({
                color: '#fff',
              }),
              fill: new Fill({
                color: '#ff3d00',
              }),
            }),
            text: new Text({
              font: '25px Arial',
              text: size.toString(),
              fill: new Fill({
                color: '#fff',
              }),
            }),
          });
        },
      });
      map.addLayer(vectorLayer);
    },
    createFeatures() {
      const pointList = [
        [118.948627, 32.120428],
        [118.96233518104327, 32.132747608006014],
        [118.958412, 32.119130],
        [118.96025438585423, 32.11912102446365],
        [118.95747155302206, 32.12247160889601],
        [118.96085782572982, 32.122218561916334],
        [118.91074424415842, 32.138229548887665],
      ];
      let list = [];
      for(let i = 0 ; i < pointList.length ; i++){
        let feature = new Feature({
          geometry: new Point(pointList[i]),
        });
        list.push(feature);
      }
      return list;
    },
  }
}
</script>
<style scoped>
#map-container {
  width: 100%;
  height: 100vh;
}
</style>
